lobsterlayui.main(['index', 'layer', 'form', 'element', 'upload'], {
    data: {
        model: {},
        sellistinfo: {},
        paytype: [],
        paymentlist: [],
        tabindex: 0,
        // apiUrl: "",
    },
    pageload: function (right) {
        console.log(right);
        console.log(parent);
        console.log(parent.sellistinfo);
        var self = this;
        self.data.sellistinfo = parent.sellistinfo;
        self.initdata();
        self.initEvent();
    },
    //获取树结构
    initdata: function () {
        var self = this;
        self.getpaytype(() => {
            var getTpl = title.innerHTML;
            var view = document.getElementById('titleview');
            layui.laytpl(getTpl).render(self.data.paytype, function (html) {
                view.innerHTML = html;
            });

            self.getpayment(() => {
                self.render();
                self.initupload();
            })
        })

    },
    //页面加载
    initEvent: function () {
        var self = this;
        layui.element.on('tab(tabBrief)', function (elem) {
            self.data.tabindex = elem.index;
            self.render();
            self.initupload();
        });
        layui.form.on('checkbox(delflag)', function (data) {
            var filterPayment = self.data.paymentlist.filter(a => { return a.PayType == self.data.paytype[self.data.tabindex].Code });
            var payment = {};
            if (filterPayment != null && filterPayment.length > 0) {
                payment = filterPayment[0];
                payment.IsUse = data.elem.checked ? 1 : 0
            } else {
                payment = self.getinitdata();
                payment.IsUse = data.elem.checked ? 1 : 0
                self.data.paymentlist.push(payment)
            }
        });
        $(document).on("blur", ".layui-input", function (e) {
            var filterPayment = self.data.paymentlist.filter(a => { return a.PayType == self.data.paytype[self.data.tabindex].Code });
            var payment = {};
            if (filterPayment != null && filterPayment.length > 0) {
                payment = filterPayment[0]
                payment[e.currentTarget.name] = e.currentTarget.value;
            } else {
                payment = self.getinitdata();
                payment[e.currentTarget.name] = e.currentTarget.value;
                self.data.paymentlist.push(payment)
            }
        })

        parent.getResult = function () {
            if (self.data.paymentlist.length == 0) {
                layer.msg("请填写数据");
                return;
            }
            return self.data.paymentlist;
        }
    },
    render: function () {
        var self = this;
        var payment = {};
        var filterPayment = self.data.paymentlist.filter(a => { return a.PayType == self.data.paytype[self.data.tabindex].Code });
        if (filterPayment == null || filterPayment.length == 0) {
            payment = self.getinitdata();
        } else {
            payment = filterPayment[0];
        }
        var getTpl = formtmp.innerHTML;
        var view = document.getElementById('box');
        layui.laytpl(getTpl).render(payment, function (html) {
            view.innerHTML = html;
            layui.form.render();
        });
    },
    getinitdata: function () {
        var self = this;
        return {
            PayType: self.data.paytype[self.data.tabindex].Code,
            PaymentId: 0,
            SubMchId: self.data.sellistinfo.SubMchId,
            AppID: "",
            AppSecret: "",
            ApiKey: "",
            MchId: "",
            NotifyUrl: "",
            BusinessNotifyUrl: "",
            RefundNotifyUrl: "",
            BusinessRefundNotifyUrl: "",
            ApiClientCert: "",
            ImgUrl: "",
            Memo: "",
            IsUse: 1,
        };
    },
    getpaytype: function (func) {
        var self = this;
        lobsterlayui.GETAPI('tspay.getdic_paytype').then(retdata => {
            self.data.paytype = retdata.data;

            func && func();
        })
    },
    getpayment: function (func) {
        var self = this;
        lobsterlayui.GETAPI('tspay.getpaymentlist', { subMchId: self.data.sellistinfo.SubMchId }).then(retdata => {
            self.data.paymentlist = retdata.data;

            func && func();
        })
    },
    initupload: function () {
        var self = this;
        layui.upload.render({
            elem: "#btnUpload",
            size: 1024,
            url: "/basic/v1/OSS/UploadFile",
            done: function (res) {
                if (res.code == 0) {
                    var fullUrl = res.data.url;
                    var filterPayment = self.data.paymentlist.filter(a => { return a.PayType == self.data.paytype[self.data.tabindex].Code });
                    if (filterPayment != null && filterPayment.length > 0) {
                        filterPayment[0]["ImgUrl"] = fullUrl;
                    } else {
                        var payment = self.getinitdata();
                        payment["ImgUrl"] = fullUrl;
                        self.data.paymentlist.push(payment)
                    }
                    self.render();
                }
            }
        });
    }
})